﻿// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "Tool/Base.h"
#include <stdio.h>
#include <stdlib.h>
#include "Api.h"

VOID readTest(DWORD pid) {
    ULONG64 moduleAddress = SH_Driver_Get_Module_Info((HANDLE)pid, (char*)"testGame.exe");
    LOG("game module address = %llx \r\n", moduleAddress);
    ULONG64 valueAddress = moduleAddress + 0x285000;
    LOG("value address = %llx \r\n", valueAddress);
    ULONG64 value = 0;
    SH_Driver_Read_Process_Memory((HANDLE)pid, (PVOID)valueAddress, &value, sizeof(value));
    LOG("value = %llx \r\n", value);
    value = 0x88888888;
    SH_Driver_Write_Process_Memory((HANDLE)pid, (PVOID)valueAddress, &value, sizeof(value));
    SH_Driver_Read_Process_Memory((HANDLE)pid, (PVOID)valueAddress, &value, sizeof(value));
    LOG("value = %llx \r\n", value);
}

void QueryMemoryTest(HANDLE pid) {
    ULONG64 moduleAddress = SH_Driver_Get_Module_Info((HANDLE)pid, (char*)"testGame.exe");
    MEMORY_BASIC_INFORMATION memoryInfo;
    SH_Driver_Query_Process_Memory((HANDLE)pid, (PVOID)moduleAddress, &memoryInfo);
    LOG("memory info:PartitionId = %llx \r\n", memoryInfo.PartitionId);
    LOG("memory info:BaseAddress = %llx \r\n", memoryInfo.BaseAddress);
    LOG("memory info:RegionSize = %llx \r\n", memoryInfo.RegionSize);
    LOG("memory info:Type = %llx \r\n", memoryInfo.Type);
    LOG("memory info:State = %llx \r\n", memoryInfo.State);
    LOG("memory info:Protect = %llx \r\n", memoryInfo.Protect);
    LOG("memory info:AllocationProtect = %llx \r\n", memoryInfo.AllocationProtect);
}

void ProtectProcessTest(HANDLE pid) {

    if (!SH_Driver_Protect_Process(pid)) {
        LOG("保护进程失败！\r\n");
    }
}



void RemoteCallTest(HANDLE pid) {

    char str[] = "asdf";
    PVOID address = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    char bufCode[] = {
        0x45,0x33, 0xC0,
        0x45,0x33, 0xC9,
        0x33,0xD2,
        0x33,0xD9,
        0x48, 0xB8, 0x89, 0x67, 0x45, 0x23, 0x01, 0x00, 0x00, 0x00,
        0x48, 0x81, 0xEC, 0xA0, 0x00, 0x00, 0x00,
        0xFF, 0xD0,
        0x48, 0x81, 0xC4, 0xA0, 0x00, 0x00, 0x00,
        0xC3
    };
    *(PULONG64)&bufCode[12] = (ULONG64)MessageBoxA;

    if (!SH_Driver_REMOTE_CALL(pid,bufCode,sizeof(bufCode))) {
        LOG("远程CALL失败！\r\n");
    }
   
}

void AsmMessageBoxTet() {

     char str[] = "asdf";
    PVOID address = VirtualAlloc(NULL, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    char bufCode[] = {
        0x45,0x33, 0xC0,
        0x45,0x33, 0xC9,
        0x33,0xD2,
        0x33,0xD9,
        0x48, 0xB8, 0x89, 0x67, 0x45, 0x23, 0x01, 0x00, 0x00, 0x00,
        0x48, 0x81, 0xEC, 0xA0, 0x00, 0x00, 0x00,
        0xFF, 0xD0,
        0x48, 0x81, 0xC4, 0xA0, 0x00, 0x00, 0x00,
        0xC3
    };
    typedef void(*testShellCode)(void);
    *(PULONG64)&bufCode[12] = (ULONG64)MessageBoxA;
    memcpy(address, bufCode, sizeof(bufCode));
    testShellCode t1 = (testShellCode)address;
    t1();
}

int main(int argc, char* argv) {

  /*if (SH_Driver_Load()) {
        printf("driver loaded success!\r\n");

        HWND hwnd;
        do {

            hwnd = FindWindowA("ConsoleWindowClass", "C:\\Program Files\\feiq\\Recv Files\\testGame.exe");
        } while (!hwnd);
        
        DWORD pid;
        GetWindowThreadProcessId(hwnd, &pid);
        LOG("pid  =%d \r\n", pid);

        RemoteCallTest((HANDLE)pid);

        SH_Driver_UnLoad();
        system("pause");
    }*/


    //TranslateFileToHFile((char*)"TestDriver.sys", (char*)"test.h");

    

    HMODULE hm = LoadLibraryA("./testDll.dll");
    FreeLibrary(hm);

    return 0;
}


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

